﻿/***
 * added by zhongrui at 20160411
 */
;jQuery(function($){
    if(!$.recoobHelper){
        $.recoobHelper = {};
    }

    function isIE89(){
       return navigator.userAgent.indexOf("MSIE 9.0") > -1
                || navigator.userAgent.indexOf("MSIE 8.0") > -1
    }

    function setImagePreview(imageElement, src) {
        if (isIE89()) {
            imageElement.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
        }
        else {
            imageElement.setAttribute('src', src);
        }
    }

    //图片预览，使用callback操作获得的callback
    $.recoobHelper.UploadFilePreview = function(fileElement, callback) {
        if (!isIE89()) {
            if (fileElement.files && fileElement.files[0]) {
                var reader = new FileReader();
                reader.onload = function (evt) {
                    var src = evt.target.result;
                    if (callback && callback instanceof Function) {
                        callback(src);
                    }
                }
                reader.readAsDataURL(fileElement.files[0]);
            }
        }
        else { //兼容IE
            fileElement.select();
            var src = document.selection.createRange().text;
            if (callback && callback instanceof Function) {
                callback(src);
            }
        }
    }

    //图片预览帮助函数
    $.recoobHelper.UploadImagePreview = function (fileElement, imageElement, callback) {
        $.recoobHelper.UploadFilePreview(fileElement, function (src) {
            setImagePreview(imageElement, src)
        });
    }

    //图片预览，ko绑定
    if (window.ko && ko.bindingHandlers) {
        ko.bindingHandlers.imgSrc = {
            update: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
                var value = ko.unwrap(valueAccessor());
                setImagePreview(element, value);
            }
        };
    }

    //发送手机验证码，并设置发送按钮的倒计时效果
    $.recoobHelper.sendValid = function (buttonSelector, url, attachData, callback) {
        //倒计时控制
        var btnRegetCode = $(buttonSelector)[0];
        if (btnRegetCode.getAttribute("disabled") == "true") {
            $.recoobHelper.showMessage("请稍后再试~");
            return false;
        }
        btnRegetCode.disabled = true;
        btnRegetCode.innerText = "(60)重新获取";
        btnRegetCode.setAttribute("disabled", "true");
        var secondTime = 60;
        function timer() {
            if (--secondTime>0) {
                btnRegetCode.innerText = "(" + secondTime + ")重新获取";
                setTimeout(timer, 1000);
            } else {
                btnRegetCode.innerText = "重新获取";
                btnRegetCode.disabled = false;
                btnRegetCode.setAttribute("disabled", "false");
            }
        }
        setTimeout(timer, 1000);

        //验证请求
        var validKey = $("[name=__RequestVerificationToken]:first()").val();
        attachData = $.extend(attachData, { "__RequestVerificationToken": !!validKey ? validKey : "" });
        $.recoobHelper.post(url, attachData, function (result) {
            if (!result.Success) {
                secondTime = 0;
                btnRegetCode.innerText = "重新获取";
                btnRegetCode.disabled = false;
                btnRegetCode.setAttribute("disabled", "false");
            }
            if (callback && callback instanceof Function) {
                callback(result);
            }
        });
    }
});